package com.cskaoyan.market.controller;

import com.cskaoyan.market.db.domain.MarketCategory;
import com.cskaoyan.market.service.AdminCategoryService;
import com.cskaoyan.market.service.impl.AdminCategoryServiceImpl;
import com.cskaoyan.market.util.JacksonUtil;
import com.cskaoyan.market.util.ResponseUtil;
import com.cskaoyan.market.vo.MarketCategoryVO;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/admin/category/*")
public class AdminCategoryController extends HttpServlet {

    private AdminCategoryService adminCategoryService = new AdminCategoryServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String requestURI = req.getRequestURI();
        String op = requestURI.replace(req.getContextPath() + "/admin/category/", "");
        //若提取路径部分为list，则进入list
        if ("list".equals(op)) {
            list(req, resp);

        }
        if ("l1".equals(op)) {
            l1(req, resp);
        }
    }




    private void list(HttpServletRequest req, HttpServletResponse resp) throws IOException {

        HttpSession session = req.getSession();
        List<MarketCategoryVO> categoryVOList = adminCategoryService.list();
        //封装成VOlist对象处理
        session.setAttribute("categoryVOList", categoryVOList);
        Object o = ResponseUtil.okList(categoryVOList);
        resp.getWriter().println(JacksonUtil.writeValueAsString(o));

    }

    private void l1(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //查level为L1的为一级类目
        List<Map<String,Object>> l1List = new ArrayList<>();
        List<MarketCategory> categoryList = adminCategoryService.l1();
        for (MarketCategory category : categoryList) {
            Map<String,Object> l1Map = new HashMap<>();
                l1Map.put("value",category.getId());
                l1Map.put("label",category.getName());
                l1List.add(l1Map);
        }

        Object o = ResponseUtil.okList(l1List);
        resp.getWriter().println(JacksonUtil.writeValueAsString(o));
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String requestURI = req.getRequestURI();
        String op = requestURI.replace(req.getContextPath() + "/admin/category/", "");
        if ("create".equals(op)) {
            create(req, resp);
        }else if ("update".equals(op)) {
            update(req,resp);
        } else if ("delete".equals(op)) {
            delete(req, resp);
        }
    }

    private void create(HttpServletRequest req, HttpServletResponse resp) throws IOException {

        String requestBody = req.getReader().readLine();
        //只有名字不为空
        String name = JacksonUtil.parseString(requestBody, "name");
        String desc = JacksonUtil.parseString(requestBody, "desc");
        String keywords = JacksonUtil.parseString(requestBody, "keywords");
        String level = JacksonUtil.parseString(requestBody, "level");
        String iconUrl = JacksonUtil.parseString(requestBody, "iconUrl");
        String picUrl = JacksonUtil.parseString(requestBody, "picUrl");
        String pid = JacksonUtil.parseString(requestBody, "pid");
        MarketCategory marketCategory = adminCategoryService.create(name, desc, picUrl,iconUrl,level,keywords,pid);
        //转换时间格式
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String addTimeToString = marketCategory.getAddTime().format(formatter);
        String UpdateTimeToString = marketCategory.getUpdateTime().format(formatter);

        Map<String, Object> data = new HashMap<>();
        data.put("id", marketCategory.getId());
        data.put("name", marketCategory.getName());
        data.put("desc", marketCategory.getDesc());
        data.put("keywords",marketCategory.getKeywords());
        data.put("level",marketCategory.getLevel());
        data.put("pid",marketCategory.getPid());
        data.put("addTime", addTimeToString);
        data.put("updateTime", UpdateTimeToString);
        if (!StringUtils.isEmpty(marketCategory.getPicUrl())) {
            data.put("picUrl", marketCategory.getPicUrl());
        }
        if (!StringUtils.isEmpty(marketCategory.getIconUrl())) {
            data.put("iconUrl", marketCategory.getIconUrl());
        }
        Object ok = ResponseUtil.ok(data);
        resp.getWriter().println(JacksonUtil.writeValueAsString(ok));

    }

    private void delete(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String requestBody = req.getReader().readLine();
        String id = JacksonUtil.parseString(requestBody, "id");
        adminCategoryService.delete(id);
        Object ok = ResponseUtil.ok();
        resp.getWriter().println(JacksonUtil.writeValueAsString(ok));
    }

    private void update(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //读取请求体中的内容
        String requestBody = req.getReader().readLine();
        String name = JacksonUtil.parseString(requestBody, "name");
        String desc = JacksonUtil.parseString(requestBody, "desc");
        String iconUrl = JacksonUtil.parseString(requestBody, "iconUrl");
        String keywords = JacksonUtil.parseString(requestBody, "keywords");
        String level = JacksonUtil.parseString(requestBody, "level");
        String picUrl = JacksonUtil.parseString(requestBody, "picUrl");
        Integer pid = Integer.valueOf(JacksonUtil.parseString(requestBody, "pid"));
        Integer id = Integer.valueOf(JacksonUtil.parseString(requestBody, "id"));
        int i = adminCategoryService.update(name,desc,iconUrl,keywords,level,picUrl,pid,id);
        Object ok = ResponseUtil.ok(i);
        resp.getWriter().println(JacksonUtil.writeValueAsString(ok));
    }
}
